package com.oocl.euc.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.oocl.euc.entity.Merchant;
import com.oocl.euc.service.MerchantService;
import com.oocl.euc.util.JSONUtil;

@Controller
public class MerchantController {
	@Autowired
	private MerchantService ms;
	
	@RequestMapping(value="/")
	public String home(){
		return "redirect:/merchantDish.html";
	}
	
	@RequestMapping(value="/merchant",method=RequestMethod.POST,produces="application/json;charset=utf-8")
	@ResponseBody
	public String addMerchant(@RequestParam String account,@RequestParam String password,HttpServletRequest req){
		Merchant m=new Merchant();
		m.setAccount(account);
		m.setPassword(password); 
		ms.merchantRegist(m);
		String result="";
		result=JSONUtil.objetc2Json(ms.findMerchantByAccount(account));
		return result;
	}
	@RequestMapping(value="/merchant",method=RequestMethod.DELETE)
	@ResponseBody
	public String deleteMerchant(@RequestBody String mid){ 
		JSONObject merchantMidJson=JSON.parseObject(mid);
		String deleteMid=merchantMidJson.getString("mid");  
		ms.deleteMerchant(deleteMid);
		return "success";
	}
	@RequestMapping(value="/merchant",method=RequestMethod.GET,produces="application/json;charset=utf-8")
	@ResponseBody
	public List<Merchant> findAllMerchant(){
		return ms.findAllMerchants();
	} 
	@RequestMapping(value="/merchantAccount",method=RequestMethod.GET)
	@ResponseBody
	public String avoidDoubleAccount(String account){
		return ms.existAccount(account)==false?"doNotExist":"exist";
	}
	
	@RequestMapping(value="/merchants",method=RequestMethod.GET)
	@ResponseBody
	public Page<Merchant> findAllMerchants(Integer limit,Integer page,String searchFilters,String sort){
		return ms.findMerchantsWithPage(limit, page, searchFilters, sort);
	} 
	
}
